package com.hk.jmm.base;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.hk.jmm.db.UserInfo;
import com.hk.jmm.helper.UserHelper;
import com.skyz.base.activity.BaseMvpActivity;
import com.skyz.base.mvp.BasePresenter;
import com.skyz.base.mvp.IModel;
import com.skyz.base.util.ScreenUtils;
import com.skyz.wrap.R;
import com.skyz.wrap.manager.TokenManager;

public abstract class BaseTitleMvpActivity<M extends IModel,
        V extends BaseTitleMvpActivity<M, V, P>,
        P extends BasePresenter<M, V>>
        extends BaseMvpActivity<M, V, P> {

    @Override
    protected void onLayoutInflated() {
        initTitleActivityView();
        initTitleContentView();
        onTitleLayoutInflated();
    }

    private LinearLayout mFlTitleContent;


    private TextView titleTextView;
    private ImageView backImageView;
    private ImageView righntImageView;
    private ViewGroup layoutTitle;

    private View click_text_right;

    public View view_father;


    private void initTitleActivityView() {
        view_father = findViewById(R.id.view_father);
        mFlTitleContent = findViewById(R.id.fl_title_content);
        titleTextView = findViewById(R.id.title_text);
        backImageView = findViewById(R.id.click_title_back);
        righntImageView = findViewById(R.id.click_title_right);
        layoutTitle = findViewById(R.id.layout_title);
        click_text_right = findViewById(R.id.click_text_right);
        click_text_right.setVisibility(View.INVISIBLE);
        backImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    public void isReportTitle(View.OnClickListener onClickListener) {
        click_text_right.setVisibility(View.VISIBLE);
        click_text_right.setOnClickListener(onClickListener);
    }




    public void clickSelect() {

    }

    public boolean getShowTakeTitleLayout() {
        return false;
    }

    @SuppressLint("ResourceAsColor")
    public void setTitleView(boolean showBack, String titleText, int rightIcon, int titleBgcolor, View.OnClickListener rightIconOnClickListener) {
        int bgcolor = ContextCompat.getColor(this, titleBgcolor > 0 ? titleBgcolor : R.color.white_color);
        setStatusBarBackgroundColor(bgcolor);
        layoutTitle.setBackgroundColor(bgcolor);
        backImageView.setVisibility(showBack ? View.VISIBLE : View.INVISIBLE);
        righntImageView.setVisibility(rightIcon > 0 ? View.VISIBLE : View.INVISIBLE);
        if (rightIcon > 0) {
            righntImageView.setImageResource(rightIcon);
            righntImageView.setOnClickListener(rightIconOnClickListener);
        }
        titleTextView.setText(titleText);
        if (titleBgcolor == R.color.white_color) {
            titleTextView.setTextColor(Color.parseColor("#212122"));
            backImageView.setImageResource(R.mipmap.icon_fanhui);
            int padding = (int) ScreenUtils.dp2px(5f);
            backImageView.setPadding(padding, padding, padding, padding);
        }
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_title;
    }

    private void initTitleContentView() {
        if (mFlTitleContent == null) {
            return;
        }
        //获取到 子类Activity 的布局id，并将布局加载进来
        int layoutId = getTitleLayoutId();
        if (layoutId == 0) {
            Log.w(TAG, "You may forget to fill in the layout id!");
            return;
        }
        mFlTitleContent.removeAllViews();
        LayoutInflater.from(mActivity).inflate(layoutId, mFlTitleContent, true);
    }

    protected abstract int getTitleLayoutId();

    protected abstract void onTitleLayoutInflated();
}
